<?php
//include("autenticacio.php");
require_once('Database.class.php');
$db = Database::getInstance();
$db->getConnection(); 

$taula = $_GET['taula'];

//header("Content-type: application/vnd.ms-excel");
header("Content-type: application/vnd.ms-excel;charset=utf-8");
header("Content-Disposition:  filename=\"$taula.xls\";");

generaExcel($taula);

include("desconnexio.php");

function generaExcel( $taula ){
 
	$query = "SELECT * FROM $taula";
	$result = mysql_query($query);
	$nbrows = mysql_num_rows($result); //files bbdd
	$nfields = mysql_num_fields($result); //columnes bbdd
	 
	if($nbrows>0){
		echo "<table border=1>";
		echo "<tr>";
		for($i=0; $i<$nfields; $i++){
			echo "<th bgcolor='gray'>".mysql_field_name($result,$i)."</th>";
		}
		echo "</tr>";
		$i=0;
		while($rec = mysql_fetch_array($result)){
			echo "<tr>";
			for($i=0; $i<$nfields; $i++){
				//echo "<th>".utf8_decode($rec[$i])."</th>";
				echo "<th>".$rec[$i]."</th>";
			}
			echo "</tr>";
		}
		echo "</table>";
	}
}
?>